package com.icefish.control;

import com.alibaba.fastjson.JSONObject;
import com.icefish.bean.Shopping_cart;
import com.icefish.bean.fastjsonParse.Meta;
import com.icefish.bean.fastjsonParse.addShopCartToOrders.addShopCartToOrdersResp;
import com.icefish.bean.fastjsonParse.addShopCartToOrders.InsertReturnInfo;
import com.icefish.dao.Shopping_cartDao;
import com.icefish.daoFactory.DaoFactory;
import com.icefish.utils.Init_Req_Resp_Config;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addShopCartToOrders")
public class addShopCartToOrders extends HttpServlet {

    private Shopping_cartDao shopping_cartDao = DaoFactory.getShopping_cartDaoInstance();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 标准配置
        Init_Req_Resp_Config.init(request, response);


        //从域中获取要添加到购物车的信息
        Integer shopping_count = Integer.valueOf(request.getParameter("shopping_count"));
        Integer shopping_amount = Integer.valueOf(request.getParameter("shopping_amount"));
        Integer goods_id = Integer.valueOf(request.getParameter("goods_id"));
        String goods_name = request.getParameter("goods_name");
        Integer goods_price = Integer.valueOf(request.getParameter("goods_price"));
        String goods_picture = request.getParameter("goods_picture");
        String goods_describe = request.getParameter("goods_describe");
        Integer user_id = Integer.valueOf(request.getParameter("user_id"));
        String username = request.getParameter("username");

        //创建对象接收收到的参数
        Shopping_cart shopping_cart = new Shopping_cart();

        shopping_cart.setShopping_count(shopping_count);
        shopping_cart.setShopping_amount(shopping_amount);
        shopping_cart.setGoods_id(goods_id);
        shopping_cart.setGoods_name(goods_name);
        shopping_cart.setGoods_price(goods_price);
        shopping_cart.setGoods_picture(goods_picture);
        shopping_cart.setGoods_describe(goods_describe);
        shopping_cart.setUser_id(user_id);
        shopping_cart.setUsername(username);


        Boolean flag = null;
        addShopCartToOrdersResp addShopCartToOrdersResp =new addShopCartToOrdersResp();
        InsertReturnInfo insertReturnInfo = new InsertReturnInfo();


        try {
            //将该对象传给dao执行
            flag = shopping_cartDao.insertGoodTOCart(shopping_cart);

            //根据商品id和用户id获取购物车信息
            insertReturnInfo = shopping_cartDao.selectReturnInfo(shopping_cart.getGoods_id(),shopping_cart.getUser_id());

            addShopCartToOrdersResp.setData(insertReturnInfo);


        } catch (Exception e) {
            e.printStackTrace();
        }

        if (flag==true){
            Meta meta = new Meta("添加购物车成功",200);
            addShopCartToOrdersResp.setMeta(meta);
        }
        else {
            Meta meta = new Meta("添加购物车失败",500);
            addShopCartToOrdersResp.setMeta(meta);
        }

        //相应Json数据
        JSONObject jsonObject = new JSONObject();
        // 转换成json
        String backToBrowser = jsonObject.toJSONString(addShopCartToOrdersResp);

        // 响应数据
        response.getWriter().write(backToBrowser);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
